JavaScript

A5.u.templateexpand Method

Syntax

A5.u.template.expand(data,settings)

Arguments

dataobject

The data to expand in the template.

settingsobjectstring

Either the template string, or an object with settings.

templatestringobject

The template string or parse template (See A5.u.template.parse) to expand.

nullValuestring

The value to use in the template for null or undefined variables or expressions.

partialsobject

An optional object the property names of which are the names of the partial templates and the values of which are the template for the given partial name.

Returns

resultstring

The expanded template.

Description

Expand a template.

Discussion

Used to expand a template with the passed in data. The passed in template can use partial templates defined in the settings. Partial templates can be useful if the template has repetitive blocks of template (such as an address block).

Example

var data = {
		firstname: 'Bob',
		lastname: 'Thomas',
		work: {
			address: '123 Main St.',
			city: 'Burlington',
			state: 'MA'
		},
		home: {
			address: '456 Maple St.',
			city: 'Burlington'
		}
	};
var welcomeStr = A5.u.template.expand(data,'Hello {firstname} {lastname}!');
// welcomeStr = 'Hello Bob Thomas!'
var profileStr = A5.u.template.expand(data,{
		template: '{firstname} {lastname}\n\nWork Address:\n{work}{*partial address}{/work}\n\nHome Address:\n{home}{*partial address}{/home}',
		nullValue: '[Missing]',
		partials: {
			address: '{address}, {city} {state}'
		}
	});
// profileStr =	'Bob Thomas
//		Work Address:
//		123 Main St., Burlington MA
//		
//		Home Address:
//		456 Maple St., Burlington [Missing]'